﻿/*----------------------------------------------------------------
 * 
 * 版权所有：Copyright (C) 2013
 * 
 * 文件名：GuidHelper.cs
 * 文件功能描述：
 * 
 * 
 * 作 者：zhangq
 * 邮 箱：ifunnyface@outlook.com
 * 日 期：2013-03-15 21:10:20
 * 
 * 
 * 修改标识： Author
 * 修改描述： 相关描述
 * 
 * ---------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{

    /// <summary>
    /// 
    /// </summary>
    public class GuidHelper
    {

        /// <summary>
        /// 
        /// </summary>
        public GuidHelper()
        {
            // TODO:
        }


        /// <summary>
        /// BASE_CHAR
        /// </summary>
        private const string BASE_CHAR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        
        /// <summary>
        /// 转换为段字符
        /// </summary>
        /// <param name="num"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private static string GetLongNo(UInt64 num, int length)
        {
            string str = "";
            while (num > 0)
            {
                int cur = (int)(num % 36);
                str = BASE_CHAR[cur] + str;
                num = num / 36;
            }
            if (str.Length > length)
            {
                str = str.Substring(str.Length - length);
            }
            else
            {
                str = str.PadLeft(length, '0');
            }

            return str;
        }

        //解析段字符
        private static UInt64 GetLongNum(string strNo)
        {
            UInt64 num = 0;
            for (int i = 0; i < strNo.Length; i++)
            {
                num += (UInt64)BASE_CHAR.IndexOf(strNo[i]) * (UInt64)Math.Pow(BASE_CHAR.Length, strNo.Length - i - 1);
            }

            return num;
        }

        /// <summary>
        /// 压缩GUID
        /// </summary>
        /// <param name="g"></param>
        /// <returns></returns>
        public static string GetGuidNo(Guid g)
        {
            string s = g.ToString().Replace("-", "").ToUpper();
            string s1 = s.Substring(0, 16);
            string s2 = s.Substring(16);
            UInt64 l1 = UInt64.Parse(s1, System.Globalization.NumberStyles.HexNumber);
            UInt64 l2 = UInt64.Parse(s2, System.Globalization.NumberStyles.HexNumber);
            string str1 = GetLongNo(l1, 13);
            string str2 = GetLongNo(l2, 13);

            return str1 + str2;
        }

        /// <summary>
        /// 获取GUID
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Guid GetGuid(string str)
        {
            if (str.Length != 26)
            {
                throw new Exception("字符串错误！长度必须是26位！");
            }
            string s1 = str.Substring(0, 13);
            string s2 = str.Substring(13);
            UInt64 l1 = GetLongNum(s1);
            UInt64 l2 = GetLongNum(s2);
            string str1 = l1.ToString("X");
            string str2 = l2.ToString("X");
            string strGuid = str1.PadLeft(16, '0');
            strGuid += str2.PadLeft(16, '0');
            Guid g = new Guid(strGuid);
            return g;
        }


    }

}